#include "StdAfx.h"
#include "xExportVertex.h"



namespace XE
{


	xExportVertex::~xExportVertex(void)
	{
	}




	bool operator==(const xExportVertex& v1,const xExportVertex v2)
	{
		if(v1.m_Posindex!=v2.m_Posindex)
			return false;
		if(v1.m_NormalIndex!=v2.m_NormalIndex)
			return false;
		//if(v1.m_UVVector.empty()&&v2.m_UVVector.empty())
		//	return true;
		//if(v1.m_ColorVector.empty()&&v2.m_ColorVector.empty())
		//	return true;
		if(v1.m_HasNormal!=v2.m_HasNormal)
			return false;
		if(v1.m_UVVector.size()!=v2.m_UVVector.size())
			return false;
		for(size_t i = 0;i<v1.m_UVVector.size();++i)
		{
			if(v1.m_UVVector[i]!=v2.m_UVVector[i])
				return false;
		}
		if(v1.m_ColorVector.size()!=v2.m_ColorVector.size())
			return false;
		for(size_t i = 0;i<v1.m_ColorVector.size();++i)
		{
			if(v1.m_ColorVector[i]!=v2.m_ColorVector[i])
				return false;
		}
		return true;

	};







	xMeshWrite::xMeshWrite(xEngineWrite* pEngineWrite,xSceneManager* pSceneMgr,  COLLADAFW::Mesh*pColladaMesh)
		:xBaseWrite(pEngineWrite),m_pSceneMgr(pSceneMgr),m_pColladaMesh(pColladaMesh)
	{

	}

	xMeshWrite::~xMeshWrite()
	{

		xMeshVertexVector::iterator it = m_MeshVertexs.begin();
		for(;it!=m_MeshVertexs.end();++it)
		{
			SafeDelete(*it)
		}

		m_MeshVertexs.clear();

		xMeshIndexsVector::iterator indexit = m_MeshIndex.begin();

		for(;indexit!=m_MeshIndex.end();++indexit)
		{
			SafeDelete(*indexit)
		}
		m_MeshIndex.clear();




	}


	bool xMeshWrite::write()
	{
		if(m_pSceneMgr==NULL||m_pColladaMesh==NULL)
			return false;

		/*	COLLADAFW::Geometry* geometry=(COLLADAFW::Geometry*)pgeometry;
		COLLADAFW::Geometry::GeometryType GeomeType =    geometry->getType();
		if(GeomeType!=COLLADAFW::Geometry::GEO_TYPE_MESH)
		return false;
		COLLADAFW::Mesh* pColladaMesh = static_cast<COLLADAFW::Mesh*>(geometry);*/
		const COLLADAFW::MeshPrimitiveArray& meshPrimitives = m_pColladaMesh->getMeshPrimitives();

		size_t FaceCount=  m_pColladaMesh->getFaceCount(COLLADAFW::MeshPrimitive::TRIANGLES);
		//bool HasNormao =   MeshNormal.getValuesCount()==0?false:true;


		//xMeshCreateParameter meshParar(pColladaMesh->getName(),FaceCount,MeshPos.getValuesCount());

		

		for ( size_t i = 0, count = meshPrimitives.getCount(); i < count; ++i )
		{

			COLLADAFW::MeshPrimitive* pmeshPrimitive = meshPrimitives[i];
			writeSubMesh(pmeshPrimitive);

		}

		return buildmesh();

	}



	bool xMeshWrite::writeSubMesh(COLLADAFW::MeshPrimitive* pmeshPrimitive)
	{
		if(pmeshPrimitive==NULL)
			return false;

		COLLADAFW::MeshPrimitive::PrimitiveType primitiveType = pmeshPrimitive->getPrimitiveType();
		if(primitiveType!=COLLADAFW::MeshPrimitive::TRIANGLES)
		{
			xLogMessager::getSingleton().logMessage\
				("conversion mesh fail ,primitive type is not TRIANGLES, mesh name");
			return false;
		}


		xSubMeshVertexs* pSubMeshVertexs = new xSubMeshVertexs();
		m_MeshVertexs.push_back(pSubMeshVertexs);

		xSubMeshIndexs* pSubMeshIndex = new xSubMeshIndexs();
		m_MeshIndex.push_back(pSubMeshIndex);


		COLLADAFW::Triangles* pTriangle = static_cast<COLLADAFW::Triangles*>(pmeshPrimitive);
		COLLADAFW::UIntValuesArray& IndexArray =   pTriangle->getPositionIndices();
		COLLADAFW::UIntValuesArray& NormalArray = pTriangle->getNormalIndices();
		COLLADAFW::IndexListArray&  ColorArray =  pTriangle->getColorIndicesArray();
		COLLADAFW::IndexListArray& UVArrary =    pTriangle->getUVCoordIndicesArray();
		xString materialname =   pmeshPrimitive->getMaterial();

		COLLADAFW::MaterialId materialid = pmeshPrimitive->getMaterialId();
	    m_MaterialId.push_back(materialid);



		size_t FaceCount = IndexArray.getCount()/3;
		bool hasNormal =! NormalArray.empty();
		size_t colorCount = ColorArray.getCount();
		size_t UVCount = UVArrary.getCount();

		size_t IndexCount = IndexArray.getCount();
		for(size_t mm = 0;mm<FaceCount;++mm)
		{

			addFace(pSubMeshVertexs,pSubMeshIndex,pTriangle,mm);

		}
		//xMeshCreateParameter  

		return true;


	}




	void xMeshWrite::addFace(xSubMeshVertexs* pSubVertex,xSubMeshIndexs* pSubIndex,\
		COLLADAFW::Triangles* pTriangles,size_t FaceIndex)
	{
		if(pSubVertex==NULL||pTriangles==NULL)
			return ;


		COLLADAFW::MeshVertexData&     MeshPos =  m_pColladaMesh->getPositions();
		size_t  PosStride =  3;//MeshPos.getStride(0);

		COLLADAFW::MeshVertexData&     MeshNormal =    m_pColladaMesh->getNormals();
		size_t  NorStride = 3; //MeshNormal.getStride(0);

		COLLADAFW::MeshVertexData&     MeshColor =      m_pColladaMesh->getColors();
		size_t  ColorStride =3; // MeshColor.getStride(0);

		COLLADAFW::MeshVertexData&     MeshUVData =  m_pColladaMesh->getUVCoords();
		size_t  UVStride =2;//  MeshUVData.getStride(0);


 

		xExportVertex v1,v2,v3;

		//---------position----------
		COLLADAFW::UIntValuesArray& IndexArray =   pTriangles->getPositionIndices();
		if(MeshPos.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_FLOAT)
		{
			v1.m_Posindex.x= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3]*PosStride];
			v1.m_Posindex.y= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3]*PosStride+1];
			v1.m_Posindex.z= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3]*PosStride+2];

			v2.m_Posindex.x= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+1]*PosStride];
			v2.m_Posindex.y= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+1]*PosStride+1];
			v2.m_Posindex.z= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+1]*PosStride+2];

			v3.m_Posindex.x= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+2]*PosStride];
			v3.m_Posindex.y= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+2]*PosStride+1];
			v3.m_Posindex.z= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+2]*PosStride+2];

		} else if(MeshPos.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_DOUBLE)
		{

			v1.m_Posindex.x= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3]*PosStride];
			v1.m_Posindex.y= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3]*PosStride+1];
			v1.m_Posindex.z= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3]*PosStride+2];

			v2.m_Posindex.x= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3+1]*PosStride];
			v2.m_Posindex.y= MeshPos.getFloatValues()->getData()[ IndexArray[FaceIndex*3+1]*PosStride+1];
			v2.m_Posindex.z= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3+1]*PosStride+2];

			v3.m_Posindex.x= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3+2]*PosStride];
			v3.m_Posindex.y= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3+2]*PosStride+1];
			v3.m_Posindex.z= MeshPos.getDoubleValues()->getData()[ IndexArray[FaceIndex*3+2]*PosStride+2];
		}


		//-----------------------normal --------------------------
		COLLADAFW::UIntValuesArray& NormalIndexArray =pTriangles->getNormalIndices();
		if(NormalIndexArray.empty()==false)
		{
			if(MeshNormal.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_FLOAT)
			{
				v1.m_HasNormal = true;
				v1.m_NormalIndex.x= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3]*NorStride];
				v1.m_NormalIndex.y= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3]*NorStride+1];
				v1.m_NormalIndex.z= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3]*NorStride+2];
				v2.m_HasNormal = true;
				v2.m_NormalIndex.x= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3+1]*NorStride];
				v2.m_NormalIndex.y= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3+1]*NorStride+1];
				v2.m_NormalIndex.z= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3+1]*NorStride+2];
				v3.m_HasNormal = true;
				v3.m_NormalIndex.x= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3+2]*NorStride];
				v3.m_NormalIndex.y= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3+2]*NorStride+1];
				v3.m_NormalIndex.z= MeshNormal.getFloatValues()->getData()[ NormalIndexArray[FaceIndex*3+2]*NorStride+2];
			}else if(MeshNormal.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_DOUBLE)
			{
				v1.m_HasNormal = true;
				v1.m_NormalIndex.x= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3]*NorStride];
				v1.m_NormalIndex.y= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3]*NorStride+1];
				v1.m_NormalIndex.z= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3]*NorStride+2];
				v2.m_HasNormal = true;
				v2.m_NormalIndex.x= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3+1]*NorStride];
				v2.m_NormalIndex.y= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3+1]*NorStride+1];
				v2.m_NormalIndex.z= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3+1]*NorStride+2];
				v3.m_HasNormal = true;
				v3.m_NormalIndex.x= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3+2]*NorStride];
				v3.m_NormalIndex.y= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3+2]*NorStride+1];
				v3.m_NormalIndex.z= MeshNormal.getDoubleValues()->getData()[ NormalIndexArray[FaceIndex*3+2]*NorStride+2];
			}

		}

		//---------------------UVMAP-----------------------------------
		size_t uvChannleCount = pTriangles->getUVCoordIndicesArray().getCount();
		v1.m_UVVector.resize(uvChannleCount);
		v2.m_UVVector.resize(uvChannleCount);
		v3.m_UVVector.resize(uvChannleCount);
		for(size_t j = 0;j<uvChannleCount;++j)
		{
			const  COLLADAFW::IndexList* pUVChannle = pTriangles->getUVCoordIndices(j);
			size_t uvStride =  pUVChannle->getStride();
			const  COLLADAFW::UIntValuesArray & uvindexarray =  pUVChannle->getIndices();
			if(MeshUVData.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_FLOAT)
			{
				v1.m_UVVector[j].x =MeshUVData.getFloatValues()->getData()[uvindexarray[FaceIndex*3]*uvStride];
				v1.m_UVVector[j].y =MeshUVData.getFloatValues()->getData()[uvindexarray[FaceIndex*3]*uvStride+1];
				v2.m_UVVector[j].x =MeshUVData.getFloatValues()->getData()[uvindexarray[FaceIndex*3+1]*uvStride];
				v2.m_UVVector[j].y =MeshUVData.getFloatValues()->getData()[uvindexarray[FaceIndex*3+1]*uvStride+1];
				v3.m_UVVector[j].x =MeshUVData.getFloatValues()->getData()[uvindexarray[FaceIndex*3+2]*uvStride];
				v3.m_UVVector[j].y =MeshUVData.getFloatValues()->getData()[uvindexarray[FaceIndex*3+2]*uvStride+1];
			}else if(MeshUVData.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_DOUBLE)
			{
				v1.m_UVVector[j].x =MeshUVData.getDoubleValues()->getData()[uvindexarray[FaceIndex*3]*uvStride];
				v1.m_UVVector[j].y =MeshUVData.getDoubleValues()->getData()[uvindexarray[FaceIndex*3]*uvStride+1];
				v2.m_UVVector[j].x =MeshUVData.getDoubleValues()->getData()[uvindexarray[FaceIndex*3+1]*uvStride];
				v2.m_UVVector[j].y =MeshUVData.getDoubleValues()->getData()[uvindexarray[FaceIndex*3+1]*uvStride+1];
				v3.m_UVVector[j].x =MeshUVData.getDoubleValues()->getData()[uvindexarray[FaceIndex*3+2]*uvStride];
				v3.m_UVVector[j].y =MeshUVData.getDoubleValues()->getData()[uvindexarray[FaceIndex*3+2]*uvStride+1];
			}


		}

		//------------vertex color------------------------------------
		size_t vertexColorCount =   pTriangles->getColorIndicesArray().getCount();
		v1.m_ColorVector.resize(vertexColorCount);
		v2.m_ColorVector.resize(vertexColorCount);
		v3.m_ColorVector.resize(vertexColorCount);
		for(size_t j = 0;j<vertexColorCount;++j)
		{
			const  COLLADAFW::IndexList* pColorList = pTriangles->getColorIndices(j);
			size_t ColorStride =  pColorList->getStride();
			const  COLLADAFW::UIntValuesArray & colorindexarray =  pColorList->getIndices();

			if(MeshColor.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_FLOAT)
			{
				v1.m_ColorVector[j].r =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3]*ColorStride];
				v1.m_ColorVector[j].g =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3]*ColorStride+1];
				v1.m_ColorVector[j].b =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3]*ColorStride+2];

				v2.m_ColorVector[j].r =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3+1]*ColorStride];
				v2.m_ColorVector[j].g =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3+1]*ColorStride+1];
				v2.m_ColorVector[j].b =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3+1]*ColorStride+2];

				v3.m_ColorVector[j].r =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3+2]*ColorStride];
				v3.m_ColorVector[j].g =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3+2]*ColorStride+1];
				v3.m_ColorVector[j].b =MeshColor.getFloatValues()->getData()[colorindexarray[FaceIndex*3+2]*ColorStride+2];



			}else if(MeshColor.getType()==COLLADAFW::FloatOrDoubleArray::DATA_TYPE_DOUBLE)
			{
				v1.m_ColorVector[j].r =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3]*ColorStride];
				v1.m_ColorVector[j].g =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3]*ColorStride+1];
				v1.m_ColorVector[j].b =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3]*ColorStride+2];

				v2.m_ColorVector[j].r =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3+1]*ColorStride];
				v2.m_ColorVector[j].g =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3+1]*ColorStride+1];
				v2.m_ColorVector[j].b =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3+1]*ColorStride+2];

				v3.m_ColorVector[j].r =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3+2]*ColorStride];
				v3.m_ColorVector[j].g =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3+2]*ColorStride+1];
				v3.m_ColorVector[j].b =MeshColor.getDoubleValues()->getData()[colorindexarray[FaceIndex*3+2]*ColorStride+2];


			}

			v1.m_ColorVector[j].a = 1.0f;
			v2.m_ColorVector[j].a = 1.0f;
			v3.m_ColorVector[j].a = 1.0f;
		}

		pSubIndex->push_back(addVertex(pSubVertex,v1));
		pSubIndex->push_back(addVertex(pSubVertex,v2));
		pSubIndex->push_back(addVertex(pSubVertex,v3));
		
	    m_FaceMaterialID.push_back(pTriangles->getMaterialId()-1);

		return ;

	}


	//----return vertex index in the sub vertex
	int  xMeshWrite::addVertex(xSubMeshVertexs* pSubVertex,const xExportVertex& v1)
	{
		if(pSubVertex==NULL)
			return -1;

		for(size_t j = 0;j<pSubVertex->size();++j)
		{
			if(pSubVertex->at(j)==v1)
			{
				return j;
			}

		}

		pSubVertex->push_back(v1);
		return (int)pSubVertex->size()-1;

	}



	bool xMeshWrite::HasNormal() const
	{
		if(m_MeshVertexs.empty()||m_MeshVertexs.at(0)->empty())
			return false;
		return m_MeshVertexs.at(0)->at(0).m_HasNormal;

	}



	size_t xMeshWrite::GetMapChannleCount()
	{
		if(m_MeshVertexs.empty()||m_MeshVertexs.at(0)->empty())
			return false;
		return m_MeshVertexs.at(0)->at(0).m_UVVector.size();

	}

	size_t xMeshWrite::GetVertexColorCount()
	{
		if(m_MeshVertexs.empty()||m_MeshVertexs.at(0)->empty())
			return false;
		return m_MeshVertexs.at(0)->at(0).m_ColorVector.size();

	}



	bool xMeshWrite::buildmesh()
	{
		//----get vertex count
		if(m_pColladaMesh==NULL)
			return false;
		size_t vertexCount = 0;
		for(size_t i = 0;i<m_MeshVertexs.size();++i)
		{
			vertexCount+=m_MeshVertexs.at(i)->size();
		}

		//------get index count;
		size_t indexCount = 0;
		for(size_t j = 0;j<m_MeshIndex.size();++j)
		{
			indexCount+=m_MeshIndex.at(j)->size();
		}


		xMeshCreateParameter  meshpara(m_pColladaMesh->getName(),indexCount/3,vertexCount);

		WORD offset = 0;
		meshpara.m_Dec.AddVertexElement(0, offset,   D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION, 0);
		if(HasNormal())
		{		
			meshpara.m_Dec.AddVertexElement(0, offset,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL, 0);
		}

		size_t vertexColorCount = GetVertexColorCount();
		if(vertexColorCount>2)
			vertexColorCount=2;
		for(size_t c = 0;c<vertexColorCount;++c)
		{
			meshpara.m_Dec.AddVertexElement(0, offset,   D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR, c);
		}


		size_t MapCount = GetMapChannleCount();
		for(size_t m = 0; m<MapCount;++m)
		{
			meshpara.m_Dec.AddVertexElement(0, offset,   D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD, m);
		}

		meshpara.m_Dec.AddEndElement();
	
		xMesh* pMesh=static_cast<xMesh*>(xMeshManager::getSingleton().CreateResource(&meshpara));
		if(pMesh==NULL)
			return  false;


		DWORD indexsize= pMesh->GetPreIndexSize();
		DWORD vertexsize = pMesh->GetPreVertexSize();

		float* pVBuffer =(float*) pMesh->LockVertexBuffer();
		WORD* pVIBuffer = (WORD*)pMesh->LockIndexBuffer();



//--------------copy vertex data-------------------------------
		for(size_t v = 0;v<m_MeshVertexs.size();++v)
		{
			xSubMeshVertexs	* pSubMeshVertex = m_MeshVertexs.at(v);
			for(size_t j = 0;j<pSubMeshVertex->size();++j)
			{
				*pVBuffer++ = pSubMeshVertex->at(j).m_Posindex.x;
				*pVBuffer++ = pSubMeshVertex->at(j).m_Posindex.y;
				*pVBuffer++ = pSubMeshVertex->at(j).m_Posindex.z;

				if(pSubMeshVertex->at(j).m_HasNormal)
				{

					*pVBuffer++ = pSubMeshVertex->at(j).m_NormalIndex.x;
					*pVBuffer++ = pSubMeshVertex->at(j).m_NormalIndex.y;
					*pVBuffer++ = pSubMeshVertex->at(j).m_NormalIndex.z;
				}

				for(size_t c = 0;c< pSubMeshVertex->at(j).m_ColorVector.size();++c)
				{
					*pVBuffer++=pSubMeshVertex->at(j).m_ColorVector.at(c).r;
					*pVBuffer++=pSubMeshVertex->at(j).m_ColorVector.at(c).g;
					*pVBuffer++=pSubMeshVertex->at(j).m_ColorVector.at(c).b;
				}

				for(size_t u = 0;u<pSubMeshVertex->at(j).m_UVVector.size();++u)
				{
					*pVBuffer++=pSubMeshVertex->at(j).m_UVVector.at(u).x;
					*pVBuffer++=pSubMeshVertex->at(j).m_UVVector.at(u).y;
				}

			}
		}

		//----------copy index data---------------------
		UINT offsetvertex = 0;
		UINT offsetindex = 0;
		for(size_t i = 0;i<m_MeshIndex.size();++i)
		{
		    xSubMeshIndexs* pSubMesIndex=m_MeshIndex.at(i);
			size_t subindexCount =pSubMesIndex->size(); 
			
			for(size_t m = 0;m<subindexCount;++m)
			{
				WORD index =(pSubMesIndex->at(m))+offsetvertex;
				*pVIBuffer++ = index;
			}



		/*	D3DXATTRIBUTERANGE atrr;
			atrr.AttribId = m_MaterialId[i]-1;
			atrr.FaceStart =offsetindex/3;
			atrr.FaceCount = subindexCount/3;
			atrr.VertexStart = _min;
			atrr.VertexCount = _max -_min+1;
			m_AttributeTableVector.push_back(atrr);*/

			offsetvertex+=m_MeshVertexs.at(i)->size();	
			offsetindex+=pSubMesIndex->size();



		}

		//----------set sub mesh-------------


		pMesh->UnLoackIndexBuffer();
		pMesh->UnLockVertexBuffer();
		
	



		XE::xEnity3DCreateParament e3dpara("texEnity+ccc",pMesh->GetName(),"texMaterial");
		XE::xEntity3D* pE3d = static_cast<XE::xEntity3D*>(\
		XE::XEngine::getSingleton().GetMainSceneManager()->CreateRenderObject(e3dpara));

		XE::xSceneNode* pParentNode = XE::XEngine::getSingleton().GetMainSceneManager()->CreateSceneNode();
		pParentNode->AttachRenderObject(pE3d);
		pMesh->SetSubMeshCount(1);
		//pMesh->SetSubMeshMaterial(0,"testMaterial0.xmat");
		//pE3d->SetMaterial("testMaterial0.xmat");
       pMesh->SetFaceMaterialId(m_FaceMaterialID);



      vertexCount =   pMesh->GetVertexNumber();
     char* pvbuffer = (char*)pMesh->LockVertexBuffer();
	 if(pvbuffer==NULL)
		 return true;
	 UINT stride = pMesh->GetPreVertexSize();
	 for(UINT  j = 0;j<vertexCount;++j)
	 {
		 xVector3 pPos =*(xVector3*)(pvbuffer); 
		 pvbuffer+=stride;
		 int cc = 0;
		 cc++;
	 }
	 pMesh->UnLockVertexBuffer();


	 indexCount  = pMesh->GetIndexNumber();
	 stride  = pMesh->GetPreIndexSize();
	 char* pIBuffer =(char*)pMesh->LockIndexBuffer();
	 for(UINT i = 0;i<indexCount;++i)
	 {
		 WORD ind = *(WORD*)pIBuffer;
		 pIBuffer+=stride;
		 int cc = 0;
		 cc++;

	 }
	 pMesh->UnLoackIndexBuffer();


	 xVertexDeclaration verdes;
	 pMesh->GetVertexDelaration(verdes);


	 m_pEngineWrite->GetUniqueIdFWMeshMap().insert(std::make_pair(m_pColladaMesh->getUniqueId(),pMesh));
		return true;

	}
}